﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class YunxingShezhiController : MonoBehaviour {

    private int i_tempYunxingShezhi=0;
    private int i_AllUIChildCount;
    public Transform tf_AllUI;
    
    // Use this for initialization
    void Start () {
        i_AllUIChildCount=tf_AllUI.childCount;
        EventManager<bool>.AddEvent(MyEventType.UpButtonClickEvent, OnUpButtonClick);
        EventManager<bool>.AddEvent(MyEventType.DownButtonClickEvent, OnDownButtonClick);
        EventManager<bool>.AddEvent(MyEventType.SetButtonClickEvent, OnSetButtonClick);
        EventManager<bool>.AddEvent(MyEventType.QuitButtonClickEvent,OnQuitButtonClick);
        EventManager<bool>.AddEvent(MyEventType.ChongzhiShujuEvent, Reset);
    }

    private void Reset(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "yunxingshezhiGO") {
            UIController._instance.ChangeGaoliang(transform.GetChild(1), i_tempYunxingShezhi,false);
            i_tempYunxingShezhi = 0;
            UIController._instance.ChangeGaoliang(transform.GetChild(1), i_tempYunxingShezhi, true);
        }
    }

    void OnUpButtonClick(bool value)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "yunxingshezhiGO")
        {
            if (i_tempYunxingShezhi > 0)
            {
                UIController._instance.ChangeGaoliang(transform.GetChild(1), i_tempYunxingShezhi, false);
                i_tempYunxingShezhi -= 1;
                UIController._instance.ChangeGaoliang(transform.GetChild(1), i_tempYunxingShezhi, true);
            }
        }
    }
    void OnDownButtonClick(bool value)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "yunxingshezhiGO")
        {
            if (i_tempYunxingShezhi < 2)
            {
                UIController._instance.ChangeGaoliang(transform.GetChild(1), i_tempYunxingShezhi, false);
                i_tempYunxingShezhi += 1;
                UIController._instance.ChangeGaoliang(transform.GetChild(1), i_tempYunxingShezhi, true);
            }
        }
    }

    void OnSetButtonClick(bool value) {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "yunxingshezhiGO") {
            if (i_tempYunxingShezhi == 1) {
                for (int i = 0; i < i_AllUIChildCount; i++)
                {
                    if (tf_AllUI.GetChild(i).name == "yabanshezhiGO")
                    {
                        
                        TimerMngr.Instance.Create(() =>
                        {
                            EventManager<bool>.TriggerEvent(MyEventType.ChongzhiShujuEvent, true);
                            tf_AllUI.GetChild(i).SetAsLastSibling();
                        }).Start(0.1f);
                        break;
                    }
                }
            }
        }
    }

    void OnQuitButtonClick(bool value)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "yunxingshezhiGO")
        {
            for (int i = 0; i < i_AllUIChildCount; i++)
            {
                if (tf_AllUI.GetChild(i).name == "mainUI")
                {
                    
                    TimerMngr.Instance.Create(() =>
                    {
                        EventManager<bool>.TriggerEvent(MyEventType.ChongzhiShujuEvent, true);
                        tf_AllUI.GetChild(i).SetAsLastSibling();
                    }).Start(0.1f);
                    
                    break;
                }
            }
        }
    }
}
